﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fes
{
    public partial class FuzzyParams : Form
    {
        public ChangeIndicator SendIndicator;
        public CancelChange CancelSendIndicator;
        private Indicator ThisInd;
        private int modyfied;
        private int IndNum;
        private bool UserMod = false;

        public FuzzyParams(Indicator thisind, int indnum)
        {
            IndNum = indnum;
            ThisInd = thisind;
            InitializeComponent();
        }

        private void FuzzyParams_Load(object sender, EventArgs e)
        {
            FuzzyList.Items.Clear();
            if(ThisInd.fuzzySets.Count==0) return;

            foreach (FuzzySet fs in ThisInd.fuzzySets) 
            {
                FuzzyList.Items.Add(fs.name + "["  + fs.Type+"]");
            }
            Dictionary<String,object> d = ThisInd.getIndicatorParams();
            nparam.Text = d["n"].ToString();

            LoadFuzzySet(0);
            UserMod = false;
            FuzzyList.SelectedIndex = 0;

        }

        private void LoadFuzzySet(int index)
        {
            modyfied = index;
            if ((index < 0) || (index >= ThisInd.fuzzySets.Count)) return;
            RealName.Text = ThisInd.fuzzySets[index].name;
            LName.Text = ThisInd.fuzzySets[index].Type;

            switch (ThisInd.fuzzySets[index].EnumType)
            {

                case FuzzySet.FuzzySetType.LeftTrapeze:

                    aVal.Enabled = false; bVal.Enabled = true; cVal.Enabled = true;
                    bVal.Text = ThisInd.fuzzySets[index].Hedge[0].ToString();
                    cVal.Text = ThisInd.fuzzySets[index].Hedge[1].ToString();
                    this.pictureBox1.Image = global::fes.Properties.Resources.leftTrapez;
                    break;
                case FuzzySet.FuzzySetType.RightTrapeze:
                    aVal.Enabled = true; bVal.Enabled = true; cVal.Enabled = false;
                    aVal.Text = ThisInd.fuzzySets[index].Hedge[0].ToString();
                    bVal.Text = ThisInd.fuzzySets[index].Hedge[1].ToString();
                    this.pictureBox1.Image = global::fes.Properties.Resources.RightTrapez;
                    break;
                case FuzzySet.FuzzySetType.Triangle:
                    aVal.Enabled = true; bVal.Enabled = true; cVal.Enabled = true;
                    aVal.Text = ThisInd.fuzzySets[index].Hedge[0].ToString();
                    bVal.Text = ThisInd.fuzzySets[index].Hedge[1].ToString();
                    cVal.Text = ThisInd.fuzzySets[index].Hedge[2].ToString();
                    this.pictureBox1.Image = global::fes.Properties.Resources.Triangle;
                    break;
                default:
                    this.pictureBox1.Image = global::fes.Properties.Resources.leftTrapez;
                    aVal.Clear(); bVal.Clear(); cVal.Clear();
                    break;
            }
            //UserMod = false;
            //FuzzyList.SelectedIndex = index;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendIndicator(ThisInd,IndNum);
            this.Close();
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if((FuzzyList.SelectedIndex==0) || (FuzzyList.SelectedIndex==FuzzyList.Items.Count-1)){
                MessageBox.Show("Nie można usunąć ograniczeń brzegowych - można je tylko zmienić");
                return;}

            ThisInd.fuzzySets.RemoveAt(FuzzyList.SelectedIndex);
            FuzzyList.Items.RemoveAt(FuzzyList.SelectedIndex);
            FuzzyList.SelectedIndex = 0;


        }

        private void Add_Click(object sender, EventArgs e)
        {
            FuzzySet fs = new FuzzySet(new double[3] { -10.0, 0.0, 10.0 }, "M", FuzzySet.FuzzySetType.Triangle);
            ThisInd.fuzzySets.Insert(ThisInd.fuzzySets.Count-1,fs);
            FuzzyList.Items.Insert(FuzzyList.Items.Count-1,fs.name + "["  + fs.Type+"]");

        }

        private void FuzzyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!UserMod) { UserMod = true; return; }
            if (FuzzyList.SelectedIndex == -1) return;
            if (!SaveFuzzySet(modyfied)) { UserMod = false; FuzzyList.SelectedIndex = modyfied; return; }
            LoadFuzzySet(FuzzyList.SelectedIndex);
        }

        private bool SaveFuzzySet(int index)
        {
            if ((index < 0) || (index >= ThisInd.fuzzySets.Count)) return false;

         

            if (aVal.TextLength == 0) aVal.Text = "0";
            if (bVal.TextLength == 0) bVal.Text = "0";
            if (cVal.TextLength == 0) cVal.Text = "0";
            if (nparam.TextLength == 0) nparam.Text = ThisInd.getIndicatorParams()["n"].ToString();
            double aValD =0.0;
            double bValD =0.0;
            double cValD =0.0;
            int nValI=0;

            try
            {
               aValD = Convert.ToDouble(aVal.Text);
               bValD = Convert.ToDouble(bVal.Text);
               cValD = Convert.ToDouble(cVal.Text);
               nValI = Convert.ToInt32(nparam.Text);            
            }
            catch (Exception) 
            {
                MessageBox.Show("Wpisane wartości są nieprawidłowe!!");
                return false;
            }

            if (nValI <= 0) { MessageBox.Show("Wartości są nieprawidłowe. N musi być większe od zera"); return false; }
            if (ThisInd.fuzzySets[modyfied].EnumType == FuzzySet.FuzzySetType.Triangle)
            {
                if ((aValD >= bValD) || (bValD >= cValD)) { MessageBox.Show("Wartości są nieprawidłowe. Założenia: a < b < c."); return false; }
                ThisInd.fuzzySets[modyfied].Hedge = new double[3] { aValD, bValD, cValD };
            }
            else if (ThisInd.fuzzySets[modyfied].EnumType == FuzzySet.FuzzySetType.LeftTrapeze)
            {
                if (bValD >= cValD) { MessageBox.Show("Wartości są nieprawidłowe. Założenia: b < c."); return false; }
                ThisInd.fuzzySets[modyfied].Hedge = new double[2] { bValD, cValD };
            }
            else if (ThisInd.fuzzySets[modyfied].EnumType == FuzzySet.FuzzySetType.RightTrapeze)
            {
                if (aValD >= bValD) { MessageBox.Show("Wartości są nieprawidłowe. Założenia: a < b."); return false; }
                ThisInd.fuzzySets[modyfied].Hedge = new double[2] { aValD, bValD };
            }
            ThisInd.fuzzySets[modyfied].name = RealName.Text;
            Dictionary<String, Object> p = ThisInd.getIndicatorParams();

            p["n"] = nValI;
            ThisInd.setInidicatorParams(p);

            return true;
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            CancelSendIndicator();
            this.Close();
        }

        private void FuzzyParams_FormClosing(object sender, FormClosingEventArgs e)
        {
            CancelSendIndicator();
        }

    
    }
}
